class Student:
    def __init__(self, id, name='', age=0, sex='男', score=0):
        self.id = id
        self.name = name
        self.age = age
        self.sex = sex
        self.score = score

    @property
    def id(self):
        return self.__id

    @id.setter
    def id(self, value):
        self.__id = value

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, value):
        self.__name = value

    @property
    def age(self):
        return self.__age

    @age.setter
    def age(self, value):
        self.__age = value

    @property
    def sex(self):
        return self.__sex

    @sex.setter
    def sex(self, value):
        self.__sex = value

    @property
    def score(self):
        return self.__score

    @score.setter
    def score(self, value):
        self.__score = value


class StudentController:
    def __init__(self):

        self.__stu_list = []

    @property
    def stu_list(self):
        return self.__stu_list[:]

    def sort_student(self):
        """
            将学生列表按照成绩升序排序
        :return:
        """
        new_list = self.__stu_list[:]
        for i in range(0, len(new_list) - 1):
            for j in range(i + 1, len(new_list)):
                if new_list[i].score > new_list[j].score:
                    new_list[i], new_list[j] = new_list[j], new_list[i]
        return new_list

    def add_student(self, student):
        """
            新增学生
        :param student:
        :return:
        """
        id = self.__generate_id()
        student.id = id
        self.__stu_list.append(student)

    def __generate_id(self):
        """
            生成学生编号
            需求：生成编号的需求：新编号，是上次添加的对象编号多1
        :return:
        """
        return self.__stu_list[-1].id + 1 if len(self.__stu_list) > 0 else 1

    def delete_student_by_id(self, id):
        for student in self.__stu_list:
            if student.id == id:
                self.__stu_list.remove(student)
                return True
        return False

    def update_student(self, stu):
        for item in self.__stu_list:
            if item.id == stu.id:
                item.name = stu.name
                item.age = stu.age
                item.score = stu.score
                return True
        return False


# student_controller = StudentController()
# student_controller.add_student(Student(1, 'Frank', 23, '男', 96))
#
# for student in student_controller.stu_list:
#     print(student.id, student.name, student.age, student.score)


class StudentManagerView:
    """
        界面视图类
    """

    def __init__(self):
        self.__controller = StudentController()

    def input_age(self):
        while True:
            try:
                age = int(input("请输入年龄："))
                return age
            except Exception as e:
                print('输入年龄有误')

    def __input_student(self):
        name = input("请输入姓名：")
        age = self.input_age()
        sex = input("请输入性别：")
        score = input("请输入成绩：")
        student = Student(None, name, age, sex, score)
        self.__controller.add_student(student)

    def __output_students(self, list_target):
        """
            显示学生信息
            :param list_target:
            :return:
            """
        for student in list_target:
            print(student.id, student.name, student.age, student.sex, student.score)

    def __output_student_by_score(self):
        list_sort = self.__controller.sort_student()
        self.__output_students(list_sort)

    def __del_student(self):
        id = int(input('请输入指定编号的学生：'))
        is_success = self.__controller.delete_student_by_id(id)
        if is_success:
            print('删除成功')
        else:
            print('删除失败')

    def __display_menu(self):
        print("(1) 添加学生")
        print("(2) 显示学生")
        print("(3) 删除学生")
        print("(4) 修改学生")
        print("(5) 按照成绩降序排序")

    def __modify_student(self):
        id = int(input('请输入需要修改的学生编号：'))
        name = input('请输入姓名：')
        age = int(input('请输入年龄：'))
        score = int(input('请输入成绩：'))
        student = Student(id, name, age, score=score)
        if self.__controller.update_student(student):
            print('修改成功')
        else:
            print('更新失败 ')

    def __select_menu(self):
        number = input('请输入选项：')
        if number == "1":
            self.__input_student()
        elif number == "2":
            self.__output_students(self.__controller.stu_list)
        elif number == "3":
            self.__del_student()
        elif number == "4":
            self.__modify_student()
        elif number == "5":
            self.__output_student_by_score()
        else:
            pass

    def main(self):
        while True:
            self.__display_menu()
            self.__select_menu()


smv = StudentManagerView()
smv.main()
